/*
 * DebugTool.c 实现了调试打印函数
 */
#include "head\DebugTool.h"

void DBG_printfDBG(uint8_t* file, uint16_t line,uint8_t *str,uint8_t len)
{
    uint8_t lineStr[6] = {0};
    uint8_t numDecimalLen;
    uint8_t i;
    uint8_t j;
    uint16_t pow = 1;
    //判断num是几位数
    for(i = 5;i > 0;i--)        //由于uint16_t最大为65535，所以直接从5位数开始判断
    {
        pow = 1;
        for(j = i-1;j > 0;j--)  //得到10的i-1次幂
        {
            pow *= 10;
        }
        if(line / pow)           //拿10的i-1次幂相除取整，如果不为零，则num有i位数
        {
            numDecimalLen = i;
            break;
        }
    }
    if(numDecimalLen == 0)
    {
        numDecimalLen = 1;
        lineStr[0] = 0x30;
    }
    else
    {
        for(i = 0;i < numDecimalLen;i++)
        {
            pow = 1;
            for(j = numDecimalLen-i-1;j > 0;j--)  //得到10的i-1次幂
            {
                pow *= 10;
            }
            lineStr[i] = (uint8_t)(line / pow); //取出十进制数第i位数字存在str
            line = line - (lineStr[i] * pow); //将十进制第i位去除
            lineStr[i] = (lineStr[i]+0x30);     //将数字x转换成ascii码x
        }
    }
    FPGA_sendStrLen("DBG,",4);
    FPGA_sendStr(file);
    FPGA_sendStrLen(":",1);
    FPGA_sendStrLen((uint8_t *)&lineStr,numDecimalLen);
    if(len!=0)
    {
        FPGA_sendStrLen(",",1);
        FPGA_sendStrLen(str,len);
    }
    FPGA_sendStrLen("\n",1);
}


